package com.lyq.controller;

import com.lyq.mapper.DepartmentMapper;
import com.lyq.mapper.DoctorMapper;
import com.lyq.mapper.HospitalMapper;
import com.lyq.pojo.Department;
import com.lyq.pojo.Doctor;
import com.lyq.pojo.Hospital;
import com.lyq.pojo.VO.DoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyq
 * @since 2020-04-23
 */
@Controller
@RequestMapping("/doctor")
public class DoctorContentController {
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    @RequestMapping("/doctorList")
    public String doctorList(Model model){
        List<DoctorVo> doctorVos = doctorMapper.SelectDoctorAll();
        model.addAttribute("doctorList",doctorVos);
        return "doctor/doctorList";

    }
    @GetMapping("/toAddDoctor")
    public String toAddDOctor(Model model){
        List<Department> departments = departmentMapper.selectList(null);
        model.addAttribute("departments",departments);
        List<Hospital> hospitals = hospitalMapper.selectList(null);
        model.addAttribute("hospitals",hospitals);
        return "doctor/addDoctor.html";
    }
    @PostMapping("/addDoctor")
    public String addDoctor(Doctor doctor){
        doctorMapper.insert(doctor);
        return "redirect:/doctor/doctorList";

    }

    @GetMapping("/toUpdateDoctor/{doctorId}")
    public String toUpdateDoctor(@PathVariable("doctorId")Integer doctorId,Model model){
        List<Department> departments = departmentMapper.selectList(null);
        model.addAttribute("department",departments);
        List<Hospital> hospitals = hospitalMapper.selectList(null);
        model.addAttribute("hospital",hospitals);
        Doctor doctor = doctorMapper.selectById(doctorId);
        model.addAttribute("doctor",doctor);
        return "doctor/updateDoctor.html";
    }
    @PostMapping("/updateDoctor")
    public String  updateDoctor(Doctor doctor){
        int byId = doctorMapper.updateById(doctor);
        return "redirect:/doctor/doctorList";
    }

    /**
     * 通过doctorId删除
     * @param doctorId
     * @return
     */
    @GetMapping("/deleteDoctor/{doctorId}")
    public String deleteDoctor(@PathVariable("doctorId")Integer doctorId){
        int i = doctorMapper.deleteById(doctorId);
        return "redirect:/doctor/doctorList";

    }




}

